<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>个人信息</title>
    <link rel="stylesheet" href="/static/userinfo.css">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<div class="profile-container">
    <h2>个人信息</h2>
    <div id="profile-info">
        <p>UID: <span id="uid"></span></p>
        <p>昵称: <span id="username"></span></p>
        <p>性别: <span id="sex"></span></p>
        <p>手机号: <span id="phonenumber"></span></p>
        <p>邮箱: <span id="email"></span></p>
        <p>地址: <span id="address"></span></p>
        <p>注册时间: <span id="create_at"></span></p>
    </div>
    <button id="logoutButton">退出登录</button>
    <button id="mypostButton">博文管理</button>
</div>
<script>
    $(document).ready(function() {
        const token = localStorage.getItem('token'); // 从 localStorage 中获取 token
        if (!token) {
            alert("未登录，请先登录！");
            window.location.href = '/index.html'; // 跳转回主页
            return;
        }

        // 发送请求获取个人信息
        fetch('/userinfo', {
            method: 'GET',
            headers: {
                'Content-Type': 'application/json',
                'Token': token
            }
        })
            .then(response => response.json())
            .then(data => {
                if (data.code === 0) {
                    // 显示个人信息
                    console.log(data)
                    $('#uid').text(data.data.uid);
                    $('#phonenumber').text(data.data.phonenumber);
                    $('#address').text(data.data.address);
                    $('#email').text(data.data.email);
                    $('#sex').text(data.data.sex);
                    $('#username').text(data.data.username);
                    $('#create_at').text(data.data.create_at);
                    // 显示其他个人信息
                } else {
                    alert('获取个人信息失败: ' + data.msg);
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('请求失败');
            });
        // 添加退出登录按钮的点击事件
        $('#logoutButton').click(function() {
            localStorage.removeItem('token'); // 移除 token
            alert("已退出登录");
            window.location.href = '/index'; // 跳转回主页
        });
        //查看自己帖子
        $('#mypostButton').click(function() {
            window.location.href = '/user/postHTML';
        });
    });
</script>
</body>
</html>